﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using BUS;
using DTO;
using System.Text;
using System.Data.SqlClient;
namespace Cucre
{
    public partial class quanlydondathang : System.Web.UI.Page
    {
        DataClasses1DataContext Data = new DataClasses1DataContext();
        string city;
        int LoaiTinKhuyenMai;
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            city = DropDownList1.SelectedValue.Trim();
            LoaiTinKhuyenMai = LoaiTinKMDropDownList.SelectedIndex;
            if (!IsPostBack)
            {
                GridViewDataBind();
                GridView_ListOrder_DataBind();
            }
        }

        public void GridViewDataBind()
        {
            switch (LoaiTinKhuyenMai)
            {
                case 0: //dang khuyen mai
                    {
                        var query = from product in Data.PRODUCTs
                                    where ((product.STATUS.Value == 1) && (product.CITY.Trim() == city.Trim()) && (product.VISIBLE == true)
                                           && ((product.TIME_START.Value - DateTime.Today).TotalSeconds < 0)
                                              && ((product.TIME_STOP.Value - DateTime.Today).TotalSeconds > 0))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        PRICE = product.PRICE,
                                        CITY = product.CITY
                                    };
                        DangKhuyenMai_GridView.DataSource = query;
                        DangKhuyenMai_GridView.DataBind();
                        DangKhuyenMai_GridView.SelectedIndex = -1;
                        GridView_ListOrder.Columns[7].Visible = false;
                        GridView_ListOrder_DataBind();
                    }
                    break;
                case 1:// da ket thuc
                    {
                        var query = from product in Data.PRODUCTs
                                    where (((product.TIME_STOP.Value - DateTime.Today).TotalSeconds < 0) &&
                                    (product.CITY.Trim() == city.Trim()) && (product.STATUS.Value == 1) && (product.VISIBLE == true))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        PRICE = product.PRICE,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        CITY = product.CITY
                                    };
                        DangKhuyenMai_GridView.DataSource = query;
                        DangKhuyenMai_GridView.DataBind();
                        DangKhuyenMai_GridView.SelectedIndex = -1;
                        GridView_ListOrder.Columns[7].Visible = false;
                        GridView_ListOrder_DataBind();
                    }
                    break;
                case 2:// chua du so phieu
                    {
                        var query = from product in Data.PRODUCTs
                                    where (((product.TIME_STOP.Value - DateTime.Today).TotalSeconds < 0) &&(product.OrderedNumber<product.MIN_NUMBER) &&
                                    (product.CITY.Trim() == city.Trim()) && (product.STATUS.Value == 1) && (product.VISIBLE == true))
                                    select new
                                    {
                                        IDPRODUCT = product.IDPRODUCT,
                                        PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                        Name_Product = product.Name_Product,
                                        PRICE = product.PRICE,
                                        TIME_START = product.TIME_START.Value.ToShortDateString(),
                                        TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                        CITY = product.CITY
                                    };
                        DangKhuyenMai_GridView.DataSource = query;
                        DangKhuyenMai_GridView.DataBind();
                        DangKhuyenMai_GridView.SelectedIndex = -1;
                        GridView_ListOrder.Columns[7].Visible = true;
                        GridView_ListOrder_DataBind();
                    }
                    break;
            }
            if (DangKhuyenMai_GridView.Rows.Count > 0)
                LabelKhuyenMai.Text = "";
            else
                LabelKhuyenMai.Text = "Không Có Tin Khuyến Mãi nào !!!!!";
        }

        public void GridView_ListOrder_DataBind()
        {
            if (DangKhuyenMai_GridView.SelectedIndex >= 0)
            {
                int IDProduct=int.Parse(DangKhuyenMai_GridView.Rows[DangKhuyenMai_GridView.SelectedIndex].Cells[0].Text);
                var query = from Order in Data.ORDERs
                            where ((Order.IDPRODUCT == IDProduct) && (Order.VISIBLE == true))
                            select new
                            {
                                IDORDER = Order.IDORDER,
                                USERNAME = Order.MEMBER.ACCOUNT.USERNAME,
                                QUATITY = Order.QUANTITY,
                                PAYMENT = Order.PAYMENT,
                                TIMEORDER = Order.TIME_ORDER
                            };
                GridView_ListOrder.DataSource = query;
                GridView_ListOrder.DataBind();
                ORDER order=new ORDER();
                for (int i = 0; i < GridView_ListOrder.Rows.Count; ++i)
                {
                    order=OrderBUS.GetOrder(int.Parse(GridView_ListOrder.Rows[i].Cells[0].Text));
                    ((DropDownList)GridView_ListOrder.Rows[i].FindControl("DropDL_Status")).SelectedIndex = (int)order.STATUS_ORDER.Value;
                }
            }
        }

        protected void DangKhuyenMai_GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            DangKhuyenMai_GridView.SelectedIndex = e.NewSelectedIndex;
            GridView_ListOrder_DataBind();
            if (GridView_ListOrder.Rows.Count > 0)
            {
                LabelProductName.Text = DangKhuyenMai_GridView.Rows[DangKhuyenMai_GridView.SelectedIndex].Cells[2].Text ;
            }
            else
            {
                LabelProductName.Text = "Chưa có đơn đặt hàng cho sản phẩm này !!!!!";
            }
        }

        protected void DangKhuyenMai_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DangKhuyenMai_GridView.PageIndex = e.NewPageIndex;
            GridViewDataBind();
        }

        protected void GridView_ListOrder_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_ListOrder.PageIndex = e.NewPageIndex;
            GridView_ListOrder_DataBind();
        }

        protected void GridView_ListOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int ID = int.Parse(GridView_ListOrder.Rows[e.RowIndex].Cells[0].Text);
                bool Result=OrderBUS.Delete(ID);              
                if (Result)
                {
                    GridView_ListOrder_DataBind();
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Qúa Trình Thực Hiện Thành Công')", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Qúa Trình Thực Hiện Thất Bại. Vui Lòng Kiểm Tra Lại và Thử Lại Lần Nửa')", true);
                }
            }
            catch (Exception ex)
            {
                string message = "alert('" + ex.Message + "')";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", message, true);
            }
        }

        protected void GridView_ListOrder_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                int ID = int.Parse(GridView_ListOrder.Rows[e.NewEditIndex].Cells[0].Text);
                ORDER EditOrder = OrderBUS.GetOrder(ID);
                EditOrder.STATUS_ORDER = (byte)((DropDownList)GridView_ListOrder.Rows[e.NewEditIndex].FindControl("DropDL_Status")).SelectedIndex;
             //  LabelProductName.Text = ((DropDownList)GridView_ListOrder.Rows[e.NewEditIndex].FindControl("DropDL_Status")).SelectedIndex.ToString();
                bool Result= OrderBUS.Update(EditOrder);
                 if (Result)
                {
                    GridView_ListOrder_DataBind();
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Qúa Trình Thực Hiện Thành Công')", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Qúa Trình Thực Hiện Thất Bại. Vui Lòng Kiểm Tra Lại và Thử Lại Lần Nửa')", true);
                }
            }
            catch (Exception ex)
            {
                string message = "alert('" + ex.Message + "')";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", message, true);
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            city = DropDownList1.SelectedValue.Trim();
            GridViewDataBind();
        }

        protected void LoaiTinKMDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoaiTinKhuyenMai = LoaiTinKMDropDownList.SelectedIndex;
            GridViewDataBind();
        }

        protected void GridView_ListOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridView_ListOrder.SelectedIndex = e.NewSelectedIndex;
            int ID = int.Parse(GridView_ListOrder.Rows[e.NewSelectedIndex].Cells[0].Text);
            ORDER EditOrder = OrderBUS.GetOrder(ID);
            EditOrder.STATUS_ORDER = 2;
            OrderBUS.Update(EditOrder);
            GridView_ListOrder_DataBind();
        }

    }
}
